<?php
    // +----------------------------------------------------------------------
    // | 海豚PHP框架 [ DolphinPHP ]
    // +----------------------------------------------------------------------
    // | 版权所有 2016~2017 河源市卓锐科技有限公司 [ http://www.zrthink.com ]
    // +----------------------------------------------------------------------
    // | 官方网站: http://dolphinphp.com
    // +----------------------------------------------------------------------
    // | 开源协议 ( http://www.apache.org/licenses/LICENSE-2.0 )
    // +----------------------------------------------------------------------

    namespace app\common\controller;

    use think\Controller;
    use think\Db;


    /**
     * 项目公共控制器
     *
     * @package app\common\controller
     */
    class Common extends Controller
    {

        /**
         * 初始化
         *
         * @author 蔡伟明 <314013107@qq.com>
         */
        protected function initialize()
        {
            // 后台公共模板
            $this->assign('_admin_base_layout', config('admin_base_layout'));
            // 后台公共模板

            // 当前配色方案
            $this->assign('system_color', config('system_color'));
            // 输出弹出层参数
            $this->assign('_pop', $this->request->param('_pop'));
        }

        /**
         * 快速验证
         *
         * @param $data
         * @param $type
         */
        protected function check_validate($data, $type)
        {

            $result = $this->validate($data, $type);
            return (true !== $result) ? $this->error($result) : $data;
        }

        /**
         * 获取筛选条件
         *
         * @author 蔡伟明 <314013107@qq.com>
         * @alter  小乌 <82950492@qq.com>
         * @return array
         */
        protected function getMap()
        {

            $search_field     = input('param.search_field/s', '');
            $keyword          = input('param.keyword/s', '');
            $filter           = input('param._filter/s', '');
            $filter_content   = input('param._filter_content/s', '');
            $filter_time      = input('param._filter_time/s', '');
            $filter_time_from = input('param._filter_time_from/s', '');
            $filter_time_to   = input('param._filter_time_to/s', '');
            $select_field     = input('param._select_field/s', '');
            $select_value     = input('param._select_value/s', '');
            $search_area      = input('param._s', '');
            $search_area_op   = input('param._o', '');

            $map = [];

            // 搜索框搜索
            if ($search_field != '' && $keyword !== '') {
                $map[$search_field] = ['like', "%$keyword%"];
            }

            // 下拉筛选
            if ($select_field != '') {
                $select_field = array_filter(explode('|', $select_field), 'strlen');
                $select_value = array_filter(explode('|', $select_value), 'strlen');
                foreach ($select_field as $key => $item) {
                    if ($select_value[$key] != '_all') {
                        $map[$item] = $select_value[$key];
                    }
                }
            }

            // 时间段搜索
            if ($filter_time != '' && $filter_time_from != '' && $filter_time_to != '') {
                $map[$filter_time] = ['between time', [$filter_time_from . ' 00:00:00', $filter_time_to . ' 23:59:59']];
            }

            // 表头筛选
            if ($filter != '') {
                $filter         = array_filter(explode('|', $filter), 'strlen');
                $filter_content = array_filter(explode('|', $filter_content), 'strlen');
                foreach ($filter as $key => $item) {
                    if (isset($filter_content[$key])) {
                        $map[$item] = ['in', $filter_content[$key]];
                    }
                }
            }

            // 搜索区域
            if ($search_area != '') {
                $search_area    = explode('|', $search_area);
                $search_area_op = explode('|', $search_area_op);
                foreach ($search_area as $key => $item) {
                    list($field, $value) = explode('=', $item);
                    $op = explode('=', $search_area_op[$key]);
                    if ($value != '') {
                        switch ($op[1]) {
                            case 'like':
                                $map[$field] = ['like', "%$value%"];
                                break;
                            case 'between time':
                            case 'not between time':
                                $value = explode(' - ', $value);
                            default:
                                $map[$field] = [$op[1], $value];
                        }
                    }
                }
            }
            $module = Db::name('admin_module')->where(['system_module' => 0])->whereNotIn('name', 'apps')->column('id,name');
            if (in_array($this->request->module(), $module)) {
                $map['app_id'] = env('app_id');
            }

            return $map;
        }

        /**
         * 获取字段排序
         *
         * @param string $extra_order 额外的排序字段
         * @param bool $before 额外排序字段是否前置
         *
         * @author 蔡伟明 <314013107@qq.com>
         * @return string
         */
        final protected function getOrder($extra_order = '', $before = false)
        {

            $order = input('param._order/s', '');
            $by    = input('param._by/s', '');
            if ($order == '' || $by == '') {
                return $extra_order;
            }
            if ($extra_order == '') {
                return $order . ' ' . $by;
            }
            if ($before) {
                return $extra_order . ',' . $order . ' ' . $by;
            } else {
                return $order . ' ' . $by . ',' . $extra_order;
            }
        }

        /**
         * 渲染插件模板
         *
         * @param string $template 模板名称
         * @param string $suffix 模板后缀
         *
         * @author 蔡伟明 <314013107@qq.com>
         * @return mixed
         */
        final protected function pluginView($template = '', $suffix = '', $vars = [], $replace = [], $config = [])
        {

            $plugin_name = input('param.plugin_name');

            if ($plugin_name != '') {
                $plugin = $plugin_name;
                $action = 'index';
            } else {
                $plugin = input('param._plugin');
                $action = input('param._action');
            }
            $suffix        = $suffix == '' ? 'html' : $suffix;
            $template      = $template == '' ? $action : $template;
            $template_path = config('plugin_path') . "{$plugin}/view/{$template}.{$suffix}";
            return parent::fetch($template_path, $vars, $replace, $config);
        }
    }